<?php
	class StoreManager extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			$this->load->model('album');
			$this->load->model('genre');
			$this->load->model('artist');
			$this->load->helper('url');
		}
		
		public function index()
		{
			if ( ! file_exists('application/views/storemanager/index.php'))
			{
				// Whoops, we don't have a page for that!
				show_404();
			}
			$data['title'] = 'Store Manager'; // Capitalize the first letter
			
			$data['album']=$this->album->get_all_album();
			$this->load->view('templates/header', $data);
			$this->load->view('storemanager/index', $data);
			$this->load->view('templates/footer', $data);
		}

		public function details($id)
		{
			$this->load->helper('form');
			if ( ! file_exists('application/views/storemanager/details.php'))
			{
				// Whoops, we don't have a page for that!
				show_404();
			}
			$data['title'] = 'Details'; // Capitalize the first letter
						
			$data['album']=$this->album->get_album_by_id($id);
			$this->load->view('templates/header', $data);
			$this->load->view('storemanager/details', $data);
			$this->load->view('templates/footer', $data);
		}
		
		public function edit()
		{
			$id=$this->uri->segment(3);
			$this->load->library('form_validation');		

			$data['title']='Edit';
			$data['albumEdit']=$this->album->get_album_by_id($id);
			$data['id']= $id;
			$data['genre']=$this->get_list_genre();					
			
			// lay artist de do vao dropdown list
			$data['artist']=$this->get_list_artist();
			
			$this->load->view('templates/header',$data);			
			if($this->form_validation->run()==FALSE)
			{
				$this->load->view('storemanager/edit',$data);
			}
			else
			{
				$this->album->update_album($this->input->post('id'),$this->get_value_from_form());
				redirect('storemanager/index','refesh');
			}
						
			$this->load->view('templates/footer');
		}		
		
		public function create()
		{
			$this->load->library('form_validation');
			$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
			
			$data['title']='Create';			
			
			// lay genre de do vao dropdown list
			$data['genre']=$this->get_list_genre();					
			
			// lay artist de do vao dropdown list
			$data['artist']=$this->get_list_artist();
				
			$this->load->view('templates/header',$data);
			
			if($this->form_validation->run()==FALSE)
			{
				$this->load->view('storemanager/create',$data);
			}			
			else
			{
				$this->album->set_album($this->get_value_from_form());
				redirect('storemanager/index','refesh');
			}
			$this->load->view('templates/footer');
				
		}
		
		public function delete()
		{
			$id=$this->uri->segment(3);
			
			$this->load->helper('form');
			if ( ! file_exists('application/views/storemanager/delete.php'))
			{
				// Whoops, we don't have a page for that!
				show_404();
			}
			$data['title'] = 'Delete'; // Capitalize the first letter
			
			$data['id']=$id;
			
			$data['album']=$this->album->get_album_by_id($id);
			$this->load->view('templates/header', $data);
			$this->load->view('storemanager/delete', $data);
			$this->load->view('templates/footer', $data);
						
		}
		
		public function confirmDelete()
		{
			$id=$this->uri->segment(3);
			$this->album->delete_album($id);
			redirect('storemanager/index','refesh');			
		}
		
		private function get_value_from_form()
		{
			$data = array(
					'GenreId' => $this->input->post('genre'),
					'ArtistId'=> $this->input->post('artist'),
					'Title'=> $this->input->post('title'),
					'Price'=> $this->input->post('price'),
					'AlbumArtUrl'=> $this->input->post('albumArtUrl')
			);
			return $data;
		}
		private function get_list_genre()
		{
			$list_genre_name = array();
			foreach ($this->genre->get_id_title_genre() as $row)
			{
				$list_genre_name[$row->GenreId] = $row->Name;
			}
			return $list_genre_name;
		}						
		private function get_list_artist()
		{
			$list_artist_name = array();
			foreach ($this->artist->get_all_artist() as $row)
			{
				$list_artist_name[$row['ArtistId']] = $row['Name'];
			}
			return $list_artist_name;
		}
	}
?>